package com.wandian.news.mvvm.passthrough;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.news.bean.ChallengeBean;
import com.wandian.news.bean.ChallengeCCYBean;
import com.wandian.news.bean.ChallengeCDMBean;
import com.wandian.news.bean.ChallengeCGGBean;
import com.wandian.news.bean.ChallengeCZMBean;
import com.wandian.news.bean.ChallengeJZWBean;
import com.wandian.news.bean.ChallengeMYDQBean;
import com.wandian.news.bean.ChallengeYZDDBean;
import com.wandian.news.bean.PassThroughBean;
import com.wandian.news.data.BaseAppApi;

import java.util.HashMap;
import java.util.Random;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_CCY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_CDM;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_CGG;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_CZM;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_JZW;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_MYDQ;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_YZDD;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description PassThroughActivityViewModel
 **/
public class PassThroughActivityViewModel extends BaseViewModel {
    private TTBaseAppApi baseAppApi;
    private BaseAppApi baseMyAppApi;
    public String url = NEWS_KNOWLEDGE_YZDD;
    public MutableLiveData<PassThroughBean> dataBean =  new MutableLiveData<>();
    public MutableLiveData<ChallengeBean> cBean =  new MutableLiveData<>();
    public PassThroughActivityViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
        baseMyAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }

    public void getData(){

        Random random  = new Random();
        int number = random.nextInt(35);
        if(number<5){
            url = NEWS_KNOWLEDGE_YZDD;
        }else if (number>=5 && number<10){
            url = NEWS_KNOWLEDGE_CGG;
        }else if (number>=10 && number<15){
            url = NEWS_KNOWLEDGE_CZM;
        }else if (number>=15 && number<20){
            url = NEWS_KNOWLEDGE_CCY;
        }else if (number>=20 && number<25){
            url = NEWS_KNOWLEDGE_MYDQ;
        }else if (number>=25 && number<30){
            url = NEWS_KNOWLEDGE_CDM;
        }else if (number>=30 && number<35){
            url = NEWS_KNOWLEDGE_JZW;
        }
        getDataUrl(url);
    }

    public void getDataUrl(String urlApic){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("num","1");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(urlApic,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        dismissDialog();
                        Log.d("passthrou",JsonUtil.toJson(o)+"");
                        if(!UIUtils.isEmpty(o)){
                            switch (urlApic){
                                case NEWS_KNOWLEDGE_YZDD:
                                    ChallengeYZDDBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeYZDDBean.class);
                                    if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(bean.getNewslist().get(0).getQuest()+"（请作答）");
                                        passThroughBean.setAnswer(bean.getNewslist().get(0).getResult());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_CGG:
                                    ChallengeCGGBean beanCGG = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeCGGBean.class);
                                    if(!UIUtils.isEmpty(beanCGG) && beanCGG.getCode()==200 && !UIUtils.isEmpty(beanCGG.getNewslist()) && beanCGG.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(beanCGG.getNewslist().get(0).getContent()+"（答一广告）");
                                        passThroughBean.setAnswer(beanCGG.getNewslist().get(0).getAnswer());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_CZM:
                                    ChallengeCZMBean beanCZM = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeCZMBean.class);
                                    if(!UIUtils.isEmpty(beanCZM) && beanCZM.getCode()==200 && !UIUtils.isEmpty(beanCZM.getNewslist()) && beanCZM.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(beanCZM.getNewslist().get(0).getContent());
                                        passThroughBean.setAnswer(beanCZM.getNewslist().get(0).getAnswer());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_CCY:
                                    ChallengeCCYBean beanCCY = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeCCYBean.class);
                                    if(!UIUtils.isEmpty(beanCCY) && beanCCY.getCode()==200 && !UIUtils.isEmpty(beanCCY.getNewslist()) && beanCCY.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(beanCCY.getNewslist().get(0).getQuestion()+"（答一成语）");
                                        passThroughBean.setAnswer(beanCCY.getNewslist().get(0).getAnswer());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_MYDQ:
                                    ChallengeMYDQBean beanMYDQ = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeMYDQBean.class);
                                    if(!UIUtils.isEmpty(beanMYDQ) && beanMYDQ.getCode()==200 && !UIUtils.isEmpty(beanMYDQ.getNewslist()) && beanMYDQ.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(beanMYDQ.getNewslist().get(0).getQuest());
                                        passThroughBean.setAnswer(beanMYDQ.getNewslist().get(0).getAnswer());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_CDM:
                                    ChallengeCDMBean beanCDM = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeCDMBean.class);
                                    if(!UIUtils.isEmpty(beanCDM) && beanCDM.getCode()==200 && !UIUtils.isEmpty(beanCDM.getNewslist()) && beanCDM.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(beanCDM.getNewslist().get(0).getRiddle()+"（"+beanCDM.getNewslist().get(0).getType()+"）");
                                        passThroughBean.setAnswer(beanCDM.getNewslist().get(0).getAnswer());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                                case NEWS_KNOWLEDGE_JZW:
                                    ChallengeJZWBean beanJZW = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),ChallengeJZWBean.class);
                                    if(!UIUtils.isEmpty(beanJZW) && beanJZW.getCode()==200 && !UIUtils.isEmpty(beanJZW.getNewslist()) && beanJZW.getNewslist().size()>0){
                                        PassThroughBean passThroughBean = new PassThroughBean();
                                        passThroughBean.setQuest(beanJZW.getNewslist().get(0).getQuest()+"（脑经急转弯）");
                                        passThroughBean.setAnswer(beanJZW.getNewslist().get(0).getResult());
                                        dataBean.setValue(passThroughBean);
                                    }
                                    break;
                            }

                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    /**
     * 更新闯关
     */
    public void notifyUserCG(int status){
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("status", status+"");
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseMyAppApi.upEmigratedNum(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("getEmigratedNum",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            ChallengeBean challengeBean = (ChallengeBean)JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),ChallengeBean.class);
                            if(!UIUtils.isEmpty(challengeBean)){
                                cBean.setValue(challengeBean);
                            }else {
                                ToastUtil.show("账号异常");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.show("网络错误");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
